#ifndef __GAMEPLACE_H__
#define __GAMEPLACE_H__

#include "Graph.h"
#include "Chip.h"

#include <cmath>
#include <iostream>
using namespace std;

class GamePlace
{
public:
	GamePlace():mPosOfZeroChip(0)
	{
		h = new Graph(16);

		h->addVertex("Zero");
		h->addVertex("One");
		h->addVertex("Two");
		h->addVertex("Three");

		h->addVertex("Four");
		h->addVertex("Five");
		h->addVertex("Six");
		h->addVertex("Seven");

		h->addVertex("Eight");
		h->addVertex("Nine");
		h->addVertex("Ten");
		h->addVertex("Eleven");
		
		h->addVertex("Twelve");
		h->addVertex("Thirteen");
		h->addVertex("Fourteen");
		h->addVertex("Fifteen");

		h->addEdge("Zero","One"); h->addEdge("Zero","Four");
		h->addEdge("One","Zero"); h->addEdge("One","Two"); h->addEdge("One","Five");
		h->addEdge("Two","One"); h->addEdge("Two","Three"); h->addEdge("Two","Six");
		h->addEdge("Three","Two"); h->addEdge("Three","Seven"); 

		h->addEdge("Four","Zero"); h->addEdge("Four","Five"); h->addEdge("Four","Eight");
		h->addEdge("Five","One"); h->addEdge("Five","Four"); h->addEdge("Five","Six"); h->addEdge("Five","Nine");
		h->addEdge("Six","Two"); h->addEdge("Six","Five"); h->addEdge("Six","Seven"); h->addEdge("Six","Ten");
		h->addEdge("Seven","Three"); h->addEdge("Seven","Six"); h->addEdge("Seven","Eleven");

		h->addEdge("Eight","Four"); h->addEdge("Eight","Nine"); h->addEdge("Eight","Twelve");
		h->addEdge("Nine","Five"); h->addEdge("Nine","Eight"); h->addEdge("Nine","Ten"); h->addEdge("Nine","Thirteen");
		h->addEdge("Ten","Six"); h->addEdge("Ten","Nine"); h->addEdge("Ten","Eleven"); h->addEdge("Ten","Fourteen");
		h->addEdge("Eleven","Seven"); h->addEdge("Eleven","Ten"); h->addEdge("Eleven","Fifteen");

		h->addEdge("Twelve","Eight"); h->addEdge("Twelve","Thirteen");
		h->addEdge("Thirteen","Nine"); h->addEdge("Thirteen","Twelve"); h->addEdge("Thirteen","Fourteen");
		h->addEdge("Fourteen","Ten"); h->addEdge("Fourteen","Thirteen"); h->addEdge("Fourteen","Fifteen");
		h->addEdge("Fifteen","Eleven"); h->addEdge("Fifteen","Fourteen"); 

		FillPlace();
		SolveTask();

	};
	void PrintChips()
	{
		int pos=0;
		for (int i = 0; i < 4; ++i) 
		{
			for (int j = 0; j < 4; ++j) 
			{
				cout << h->getVertex(pos)->getChip( )->getLabel() << " ";
				++pos;
			}
			cout << endl;
		}
		cout << endl;
		
	}

	void SolveTask()
	{
		h->printAdjMatr();
		int pCurrPosOfChip1 = SearchVertexByChip(10);
		int pNeedPosOfChip1 = 0;
		int pCurrPosOfZero	= SearchVertexByChip(0);
		
		cout << pCurrPosOfChip1 << " " << pNeedPosOfChip1 << " " << pCurrPosOfZero << endl;

		h->dfs(h->getVertex(pCurrPosOfChip1)->getLabel(), h->getVertex(pNeedPosOfChip1)->getLabel());
		h->printStack();
	}
/*
|X|.|.|.|
|.|.|.|1|
|0|.|.|.|
|.|.|.|.|
*/
	int SearchVertexByChip(int pChipLabel)
	{
		int pos=0;
		for (int i = 0; i < 4; ++i) 
		{
			for (int j = 0; j < 4; ++j) 
			{
				if ( h->getVertex(pos)->getChip( )->getLabel() == pChipLabel )
				{
					return pos;
				}
				++pos;
			}
		}

	}


	virtual ~GamePlace()
	{
		delete h;
	};

private:
	Graph* h;
	
	int mPosOfZeroChip;

	void FillPlace()
	{
		int** pArray;
		pArray = new int*[4];
		for (int i = 0; i < 4; ++i) 
		{
			pArray[i] = new int[4];
			for (int j = 0; j < 4; ++j) 
			{
				pArray[i][j] = 0;
			}
		}
		doRandomArray(pArray,16);
		int pos=0;
		for (int i = 0; i < 4; ++i) 
		{
			for (int j = 0; j < 4; ++j) 
			{
				h->getVertex(pos)->setChip( new Chip(pArray[i][j]) );
				if ( pArray[i][j] == 0 )
				{
					mPosOfZeroChip = pos;
				}
				++pos;
			}
		}
	}

	bool checkArr(int* pTmpArr, int size, int num)
	{
		if ( num >= size )
		{
			return false;
		}
		else if (pTmpArr[num] == -1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	void doRandomArray(int**& pArray, int size)
	{
		int _Arr[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
		int tmpArr[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
		int  tmp = 0;
		bool doNext = true;

		for (int i = 0; i < 4; ++i)
		{
			for (int j = 0; j < 4; ++j)
			{
				do
				{
					tmp = int( rand() % 100);
					if ( checkArr(tmpArr, size, tmp) )
					{
						tmpArr[tmp]	= _Arr[tmp];
						pArray[i][j] = _Arr[tmp];
						doNext = false;
					}
					else
					{
						doNext = true;
					}
				}
				while(doNext);
			}
		}
	};

};

#endif	__GAMEPLACE_H__